/*///////////////////////////////////////////////////////////////////
VBMath_raw.h
VBMath - The Villas-Boas library for mathematics in C++ cross platform
LGPL licenced
VBMath is part of sbvblibs

Download sbvblibs from link below
http://code.google.com/p/sbvblibs/

http://www.sbvb.com.br 
by Sergio B. Villas-Boas (sbvb@poli.ufrj.br)
///////////////////////////////////////////////////////////////////*/

#ifndef __VBATH_H
#define __VBATH_H


// all entities in namespace br::com::sbVB::VBMath
namespace br {
namespace com {
namespace sbVB {
namespace VBMath {

typedef unsigned char byte;

////////////////////////////////////////////////////////////////////
// global functions complex additions
/////////////////////////////////////////////////////////////////////

template <typename T>
bool operator>(std::complex<T> x, std::complex<T> y)
{
	return complexAbs(x) > complexAbs(y);
}

template <typename T>
bool operator>=(std::complex<T> x, std::complex<T> y)
{
	return complexAbs(x) >= complexAbs(y);
}

}}}} // close namespace ::br::com::sbVB::VBMath



////////////////////////////////////////////////////////////////////
// integer math global functions
/////////////////////////////////////////////////////////////////////

namespace br {
namespace com {
namespace sbVB {
namespace VBMath {

template <typename IntType>
void createPrimesContainer(std::deque<IntType> & primesContainer,IntType limit)
{
	typename std::deque<IntType>::iterator it;
	primesContainer.push_back(2); // first hardcoded prime
	primesContainer.push_back(3); // second hardcoded prime
	IntType currNumber = 3;
	if (limit > 3)
	{
		// add primes until limit
		while (currNumber <= limit)
		{
			bool currNumberIsPrime = true;
			for (it = primesContainer.begin(); it != primesContainer.end(); it++)
			{
				if (currNumber % *it == 0)
				{
					currNumberIsPrime = false;
					break; // noo need to keep searching for currNumber
				}
			}
			if (currNumberIsPrime)
			{
				primesContainer.push_back(currNumber);
				// cout << "DEBUG: added prime: " << currNumber << endl;
			}
			currNumber += 2;
		}
	}
};

	
template <typename IntType>
void createPrimesFile(IntType limit, const char *fileName)
{
	std::deque<IntType> primesContainer; // list of primes in memory
	typename std::deque<IntType>::iterator it;
	createPrimesContainer(primesContainer,limit);

	// save primes to file
	std::ofstream primesFile;
	primesFile.open(fileName);
	if (!primesFile)
		throw VBMathException(VBString("could not open file ") + VBString(fileName));
	for (it = primesContainer.begin(); it != primesContainer.end(); it++)
	{
		primesFile << *it << std::endl;
	}
};

template <typename IntType>
void loadPrimesFile(const char *fileName, std::deque<IntType> & primesContainer)
{
	std::ifstream primesFile;
	primesFile.open(fileName);
	if (!primesFile)
		throw VBMathException(VBString("could not open file ") + VBString(fileName));
	IntType currPrime;
	while (true)
	{
		primesFile >> currPrime;
		if (primesFile.eof()) break;
		primesContainer.push_back(currPrime);
	}
};


}}}} // close namespace ::br::com::sbVB::VBMath

#endif // __VBMATH_H






